export default function maxProfitComp(prices: number[]): number {
  const n = prices.length;
  if (n <= 1) return 0;

  let preValue = 0;
  let minPrice = prices[0];
  for (let i = 1; i < n; i++) {
    // i位置的值，之和前一个位置有关，因此可以压缩
    preValue = Math.max(prices[i] - minPrice, preValue);
    minPrice = Math.min(prices[i], minPrice);
  }

  return preValue;
}

console.log(maxProfitComp([7, 1, 5, 3, 6, 0, 4]));
